package newWork.controller;

import newWork.constant.ResultConstant;
import newWork.entity.*;
import newWork.service.IOptionService;
import newWork.service.IProjectService;
import newWork.service.IStaffProjectService;
import newWork.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/option")
public class OptionController {

	@Autowired
	private IOptionService service;

	@Autowired
	private IProjectService projectService;

	@Autowired
	private IStaffService staffService;

	@Autowired
	private IStaffProjectService staffProjectService;

	/**
	 * 查询
	 *
	 * @return
	 */
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public CommonResult select(@RequestBody OptionEntity entity) {
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, service.select(entity));
	}


	/**
	 * 查询列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public LayUITable<OptionEntity> list1(Integer page, Integer limit) {
		Integer newPage = (page-1)*limit;
		OptionEntity optionEntity1 = new OptionEntity();
		optionEntity1.setPageSize(limit);
		optionEntity1.setStart(newPage);
		int count = Integer.parseInt(service.likeSelectCount(new OptionEntity()).toString());
		List<OptionEntity> select = service.select(optionEntity1);
		for (OptionEntity optionEntity : select) {
			ProjectEntity projectEntity = new ProjectEntity();
			projectEntity.setProjectId(optionEntity.getProjectId());
			ProjectEntity projectEntity1 = projectService.select(projectEntity).get(0);
			optionEntity.setProjectEntity(projectEntity1);
		}
		return new LayUITable<>(0, "SUCCESS", count, select);
	}


	/**
	 * 查询列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/list2", method = RequestMethod.GET)
	public LayUITable<OptionEntity> list2(HttpSession session) {
		//获取session中的user从而得到staffID
		StaffEntity staff = (StaffEntity)session.getAttribute("user");
		Integer staffId = staff.getStaffId();
		StaffProjectEntity staffProjectEntity = new StaffProjectEntity();
		staffProjectEntity.setStaffId(staffId);
		//通过员工-项目表获取到项目
		List<StaffProjectEntity> staffProjectEntityList = staffProjectService.select(staffProjectEntity);
		Set<Integer> set = new HashSet<>();
		for (StaffProjectEntity entity : staffProjectEntityList) {
			//获取到这个员工的项目ID并去重
			set.add(entity.getProjectId());
		}
		List<OptionEntity> list = new ArrayList<>();
		//遍历去重后的项目id进行查询项目信息
		for (Integer integer : set) {
			OptionEntity optionEntity = new OptionEntity();
			optionEntity.setProjectId(integer);
			List<OptionEntity> select = service.select(optionEntity);
			for (OptionEntity entity : select) {
				ProjectEntity projectEntity = new ProjectEntity();
				projectEntity.setProjectId(entity.getProjectId());
				ProjectEntity projectEntity1 = projectService.select(projectEntity).get(0);
				entity.setProjectEntity(projectEntity1);
				list.add(entity);
			}
		}
		return new LayUITable<>(0, "SUCCESS", list.size(), list);
	}

	/**
	 * 更新
	 *
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CommonResult update(@RequestBody OptionEntity entity) {
		StaffEntity staffEntity = new StaffEntity();
		staffEntity.setStaffId(entity.getStaffId());
		List<StaffEntity> select = staffService.select(staffEntity);
		if (select ==null || select.size() == 0){
			return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG,"该员工不存在");
		}
		ProjectEntity projectEntity = new ProjectEntity();
		projectEntity.setProjectId(entity.getProjectId());
		List<ProjectEntity> select1 = projectService.select(projectEntity);
		if (select1 ==null || select1.size() == 0){
			return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG,"该项目不存在");
		}
		service.update(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 添加
	 *
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public CommonResult add(@RequestBody OptionEntity entity) {
		//判断该项目是否存在
		ProjectEntity projectEntity = new ProjectEntity();
		projectEntity.setProjectId(entity.getProjectId());
		List<ProjectEntity> select1 = projectService.select(projectEntity);
		if (select1 ==null || select1.size() == 0){
			return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG,"该项目不存在");
		}
		//判断该员工是否存在
		StaffEntity staffEntity = new StaffEntity();
		staffEntity.setStaffId(entity.getStaffId());
		List<StaffEntity> select = staffService.select(staffEntity);
		if (select ==null || select.size() == 0){
			return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.FAIL_MSG,"该员工不存在");
		}
		service.add(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 删除
	 *
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public CommonResult delete(@RequestBody OptionEntity entity) {
		service.delete(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

}
